package server.core;

import lombok.Data;
import lombok.SneakyThrows;
import org.dom4j.Node;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Data
public class Host {

    private String appBase;
    private String name;

    private List<Context> appList;

    public Host(Node node) {
        appBase = node.valueOf("@appBase");
        name = node.valueOf("@name");
    }

    @SneakyThrows
    public void start() {
    }

    @SneakyThrows
    public void init() {
        appList = new ArrayList<>();
        Path webappsPath = Paths.get(appBase);
        if (!Files.exists(webappsPath)) {
            throw new RuntimeException("不存在目录appBase : " + webappsPath + "  检查Server.xml配置");
        }
        Files.list(webappsPath).filter(Files::isDirectory).forEach(path -> {
            Context app = new Context();
            app.init(path);
            appList.add(app);
        });
    }
}
